package com.spring.web.adminmanage.controller;

import com.spring.web.adminmanage.entities.Role;
import com.spring.web.adminmanage.service.CommonsWeatherService;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 首页天气预报接口
 * Created by hetao on 2017/9/18
 *
 */
@Controller
@RequestMapping("/commonsWeather")
public class CommonsWeatherController {
    private static Logger logger = LoggerFactory.getLogger(CommonsWeatherController.class);
	@Autowired
	private CommonsWeatherService commonsWeatherService;

	@RequestMapping("/getWeather")
	@ResponseBody
	public String getWeather(Model model, String city, HttpServletRequest request) {
		Role role = (Role) request.getSession().getAttribute("roleData");
        Map<String,String> resultMap=null;
		try {
			resultMap=	commonsWeatherService.getResultMap(city);
			resultMap.put("userName", role.getRolename());
		} catch (Exception e) {
			logger.error("获得天气信息失败");
			e.getStackTrace();
		}
		return JSONArray.fromObject(resultMap).toString();
	}
	

}
